package com.cloudsigns.factorymanager2.project.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringInstallCompletionOrderForSuperActivity;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringInstallCompleteForSuperPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringInstallOrderDetailPresenter;
import com.cloudsigns.factorymanager2.project.bean.SurVeyOrderBean;
import com.cloudsigns.factorymanager2.project.bean.SurveyFeedbackContentBean;
import com.cloudsigns.factorymanager2.project.iview.ISurveyFeedbackConfirmForManagerView;
import com.cloudsigns.factorymanager2.project.iview.ISurveyOrderDetailView;
import com.cloudsigns.factorymanager2.project.presenter.SurveyFeedbackConfirmForManagerPresenter;
import com.cloudsigns.factorymanager2.project.presenter.SurveyOrderDetailPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

/**
 * Created by tony on 2018/8/5.
 */

public class SurveyCompletionOrderForProjectActivity extends Activity implements ISurveyOrderDetailView, ISurveyFeedbackConfirmForManagerView, View.OnClickListener {
    private TextView tv_title;
    private ViewGroup rl_back;
    private final String TAG = SurveyCompletionOrderForProjectActivity.class.getSimpleName();
    private TextView tv_project_name, tv_engineering_name;
    private View rl_project;
    private TextView edt_tijiaorenn, edt_submit_date;
    private TextView btn_confirm_evaluation;
    private LinearLayout ll_engineering_info_content;
    private ImageView img_project_select;
    private boolean isclickable;
    private SurveyOrderDetailPresenter surveyOrderDetailPresenter;
    private OrderParentBean orderParentBean;
    private SurVeyOrderBean surVeyOrderBean;
    private boolean isCLick;
    private SurveyFeedbackConfirmForManagerPresenter surveyFeedbackConfirmForManagerPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_survey_completion_order_for_project);

        surveyOrderDetailPresenter = new SurveyOrderDetailPresenter(this);
        surveyFeedbackConfirmForManagerPresenter = new SurveyFeedbackConfirmForManagerPresenter(this);
        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {
            orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_1);
        }

        surveyOrderDetailPresenter.surveyOrderDetail();

    }

    private void init() {
        tv_title = findViewById(R.id.tv_title);
        rl_back = findViewById(R.id.rl_back);
        tv_project_name = findViewById(R.id.tv_project_name);
        tv_engineering_name = findViewById(R.id.tv_engineering_name);
        rl_project = findViewById(R.id.rl_project);
        ll_engineering_info_content = findViewById(R.id.ll_engineering_info_content);
        edt_tijiaorenn = findViewById(R.id.edt_tijiaorenn);
        edt_submit_date = findViewById(R.id.edt_submit_date);
        btn_confirm_evaluation = findViewById(R.id.btn_confirm_evaluation);
        img_project_select = findViewById(R.id.img_project_select);

        tv_title.setText("现场勘察");
//        btn_confirm_evaluation.setVisibility(View.GONE);

        rl_back.setOnClickListener(this);
        btn_confirm_evaluation.setOnClickListener(this);
        rl_project.setOnClickListener(this);

    }

    private void initData() {
        if (null != surVeyOrderBean) {
            tv_project_name.setText(surVeyOrderBean.getProjectName());
            tv_engineering_name.setText("/" + surVeyOrderBean.getEngineeringName());
            edt_tijiaorenn.setText(surVeyOrderBean.getAssignReceiveAccountName());
//            edt_submit_date.setText(DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getSendOrderTime()));

            CommonEnum.CompleteStatus completeStatus = surVeyOrderBean.getCompleteStatus();
            if (null != completeStatus) {
                if (completeStatus == CommonEnum.CompleteStatus.WORKING) {
                    btnClicable(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.WORKER_COMPLETE) {
                    btnClicable(true);
                } else if (completeStatus == CommonEnum.CompleteStatus.ADMIN_COMPLETE) {
                    btnClicable(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.SUPERADMIN_COMPLETE) {
                    btnClicable(false);
                }
            }


        }
    }

    private void btnClicable(boolean bool) {
        isCLick = bool;
        if (bool) {
            btn_confirm_evaluation.setBackgroundResource(R.mipmap.btn_long_bg_green);
            btn_confirm_evaluation.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        } else {

            btn_confirm_evaluation.setBackgroundResource(R.mipmap.btn_bg_short_gray);
            btn_confirm_evaluation.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        }
    }


    private void submit() {
        surveyFeedbackConfirmForManagerPresenter.surveyFeedbackConfirmForManager();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.rl_project:
                showEngineeringInfoDialog();
                break;
            case R.id.btn_confirm_evaluation:
                if (isCLick)
                    submit();
                break;
        }
    }


    private void showEngineeringInfoDialog() {
        if (ll_engineering_info_content.getVisibility() == View.VISIBLE) {
            ll_engineering_info_content.setVisibility(View.GONE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_engineering_info_content.setVisibility(View.VISIBLE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    @Override
    public long getFeedbackDate() {
        return 0;
    }

    @Override
    public String getItemId() {
        return null;
    }

    @Override
    public String getcontent() {
        return null;
    }

    @Override
    public String getdailyFeedbackId() {
        if(null != surVeyOrderBean){
            SurveyFeedbackContentBean surveyFeedbackContentBean = surVeyOrderBean.getSurveyFeedbackContentBean();
            if(null != surveyFeedbackContentBean)
                return  surveyFeedbackContentBean.getId();
        }
        return null;
    }

    @Override
    public CommonEnum.EngineeringORderCategory getType() {
        return CommonEnum.EngineeringORderCategory.SURVEY;
    }

    @Override
    public void surveyFeedbackConfirmForManagerSuccess() {
        Toast.makeText(this, "提交成功", Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        setResult(RESULT_OK, mIntent);
        finish();
    }

    @Override
    public void surveyFeedbackConfirmForManagerFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public String getOrderId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return "";
    }

    @Override
    public void surveyOrderDetailSuccessed(SurVeyOrderBean orderBean) {
        surVeyOrderBean = orderBean;
        initData();
    }

    @Override
    public void surveyOrderDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
